﻿Ext.define('Oa.EmpProfileGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'Widget.EmpProfileGrid',

    // override
    initComponent: function () {
        var store = Ext.create('Ext.data.TreeStore', {
            model: 'TreeModel',
            proxy: {
                type: 'ajax',
                url: '/dept.mvc/DeptTreeByEmp',
                extraParams: {
                    controllerName: 'EmpProfile'
                },
                reader: {
                    type: 'json'
                }
            },
            // 设置根节点
            root: {
                expanded: true
            },
            autoLoad: true
        });

        this.columns = [
			{
			    header: "工号", width: 100,
			    dataIndex: 'JobNo'
			}, {
			    header: "姓名", width: 100,
			    dataIndex: 'Name'
			}, {
			    header: "部门", width: 130,
			    dataIndex: 'Dep'
			}, {
			    header: "分机", width: 80,
			    dataIndex: 'TelExt'
			}, {
			    header: "手机", width: 120,
			    dataIndex: 'Mobile'
			}, {
			    header: '身份证地址', flex: 1,
			    dataIndex: 'IdAddress'
			}, { header: "操作", dataIndex: 'Id', width: 130,
			    renderer: function (value) {
			        var links = [];
			        links.push('<a href="EmpInfo.aspx?empId=' + value + '" target="_blank">查看</a>');
			        //超级管理员Id 2
			        if (",2,".indexOf("," + Cookies.get("empId") + ",") >= 0) {
			            links.push('<a href="#" onclick="var url =\'EmpEdit.aspx?empId=' + value + '\';window.showModalDialog(url, window, \'dialogWidth=850px;dialogHeight=650px;status=no;help=no;resizable=yes;\');Ext.getCmp(\'EmpProfileMgr\').getStore().reload();">编辑</a>');
			        }
			        return links.join("&nbsp;");
			    }
			}
		];
        // Set up a model to use in our Store
        Ext.define('EmpProfileModel', {
            extend: 'Ext.data.Model',
            fields: [{
                name: 'Id'
            }, {
                name: 'JobNo'
            }, {
                name: 'Name'
            }, {
                name: 'DeptId'
            }, {
                name: 'Dep'
            }, {
                name: 'Mobile'
            }, {
                name: 'TelExt'
            }, {
                name: 'ShortNo'
            }, {
                name: 'IdAddress'
            }]
        });

        var ds = Ext.create('Ext.data.Store', {
            model: 'EmpProfileModel',
            pageSize: 20, // items per page
            proxy: {
                type: 'ajax',
                url: '/EmpProfile.mvc/Find',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount'//分页使用
                }
            },
            autoLoad: false
        });
        ds.load({ params: { start: 0, limit: 20} });
        ds.on('beforeload', function (thiz, options) {
            var statusId = Ext.getCmp('StatusId').getValue();
            Ext.apply(thiz.proxy.extraParams, { deptId: Ext.getCmp('treeDeptEmpProfile').getValue(), name: Ext.getCmp('txtNameEmpProfile').getValue(), statusId: statusId });
        });
        this.store = ds;

        this.dockedItems = [{
            xtype: 'toolbar',
            dock: 'top',
            items: [{
                xtype: 'tbtext',
                text: '部门:'
            }, {
                xtype: 'treepicker',
                id: 'treeDeptEmpProfile',
                displayField: 'text',
                forceSelection: true,
                editable: false,
                width: 250,
                store: store
            }, {
                xtype: 'tbtext',
                text: '姓名:'
            }, {
                xtype: 'textfield',
                id: 'txtNameEmpProfile',
                emptyText: '人名的一或几个字',
                blankText: '人名的一或几个字',
                anchor: '100%'
            },{
                xtype: 'tbtext',
                text: '在职状态:'
            }, {
                xtype: 'combo',
                id: 'StatusId',
                displayField: 'text',
                valueField: 'id',
                mode: 'local',
                editable: false,
                selectOnFoucs: true,
                triggerAction: 'all',
                blankText: '请选择在职状态',
                emptyText: '请选择在职状态',
                store: new Ext.data.Store({ fields: ['id', 'text'],
                    data: [{ 'id': 0, 'text': '全部' }, { 'id': 1, 'text': '在职' }, { 'id': 2, 'text': '离职' }, { 'id': 3, 'text': '试用期' }, { 'id': 4, 'text': '实习期'}], autoload: true
                }),
                listeners: { scope: this }
            }, {
                text: "查询",
                iconCls: 'icon-search',
                handler: function () {
                    Ext.getCmp('pagingEmpProfile').moveFirst();
                }
            }, '-', {
                text: "添加",
                tooltip: "添加新的信息",
                iconCls: "addicon",
                handler: function () {
                    var url = 'EmpBasicInfoTab.aspx';
                    var retVal = window.showModalDialog(url, window, 'dialogWidth=780px;dialogHeight=560px;status=no;help=no;resizable=yes;');
                    if (!isNaN(retVal)) {
                        url = 'EmpEdit.aspx?empId=' + retVal;
                        alert('新增成功,请继续填写其他资料或关闭窗口!');
                        window.showModalDialog(url, window, 'dialogWidth=850px;dialogHeight=650px;status=no;help=no;'); //检测数据更改>刷新?
                        ds.reload();
                    }
                }
            }]
        }, {
            xtype: 'pagingtoolbar',
            id: 'pagingEmpProfile',
            store: ds,
            dock: 'bottom',
            displayInfo: true
        }];

        // finally call the superclasses implementation
        Oa.EmpProfileGrid.superclass.initComponent.call(this);
    }/*,
    render: function () {
        Oa.EmpProfileGrid.superclass.render.apply(this, arguments);
        //超级管理员Id 2
        if (",2,".indexOf("," + Cookies.get("empId") + ",") == -1) {
            this.dockedItems.items[1].items.items[7].hide();
            this.dockedItems.items[1].items.items[8].hide();
        }
    }*/
});